		
;(function(win,$){
	var OTable = function(obj){
		this.target = obj;
		this.hasCaption = this.target.find("caption").length>0?true:false;
		this.hasThead = this.target.find("thead").length>0?true:false;
		this.hasTfoot = this.target.find("tfoot").length>0?true:false;
		this.initStyle();
		this.initParm();
		this.tableWraper = $("<div class='maxTableWarper'></div>"); 
		this.toolbar = $("<div class='maxTable_toolbar'></div>");
		this.caption = $("<div class='maxTable_caption'></div>");
		this.container = $("<div class='maxTable_container'></div>");
		this.header = $("<div class='maxTable_header'></div>");
		this.body = $("<div class='maxTable_body'></div>");
		this.footer = $("<div class='maxTable_footer'></div>");
		this.pagination = $("<div class='maxTable_pagination'></div>");
		this.getClass();
		this.getId();
		this.iniTable();
	}
	OTable.CSSSTYLE = "<style id='tableInitSty'>"+
						".maxTable_container {position: relative;border: 1px solid #ddd;}"+
						".maxTable_header{position:absolute;top:0;left:0;right:0px;background:#ebebeb;}"+
						".maxTable_caption>.table,.maxTable_header>.table,.maxTable_body>.table,.maxTable_footer>.table{margin-bottom:0px!important;}"+
						".maxTable_caption{margin-bottom:-1px;}"+
						".maxTable_body{overflow-y:auto;min-height:140px;}"+
						".maxTable_footer{position:absolute;left:0;right:0px;bottom:0px;background:#ebebeb;}"+
						".maxTable_pagination{padding-top:6px;clear:both;zoom:1;overflow:hidden;}"+
						".maxTable_pagination .foot_nav{position:static;}"+
						"caption{background:#ddd;color:#333;text-indent:0.5em;}"+
					  "</style>";
	OTable.prototype = {
		initStyle:function(){
			var head = $(document).find("head")
			if(head.find("style#tableInitSty").length == 0){
				head.append(OTable.CSSSTYLE);
			}
		},
		//获取对象的类样式
		getClass:function(){
			var self = this;
			this.classes =  $(self.target).attr("class");
		},
		//获取对象的Id
		getId:function(){
			var self = this;
			this.id =  $(self.target).attr("id");
		},
		//获取caption的模版
		getCaptionTmp:function(){
			var self = this;
			self.captionTmp = self.target.find("caption").clone(); 
		},
		//创建表格标题
		creatCaption:function(){
			var self = this;
			var ctable = $("<table></table>");
			self.getCaptionTmp();
			ctable.addClass(self.classes);
			ctable.append(self.captionTmp);
			self.caption.append(ctable);
		},
		//获取thead的内容
		getHeaderTmp:function(){
			var self = this;
			self.headerTmp = self.target.find("thead").clone(); 
		},
		//创建thead
		creatHeader:function(){
			var self = this;
			var htable = $("<table></table>");
			self.getHeaderTmp();
			htable.addClass(self.classes);
			htable.append(self.headerTmp);
			self.header.append(htable);
		},
		getBodyTmp:function(){
			var self = this;
			self.bodyTmp = self.target.html();
		},
		//创建tbody
		creatBody:function(){
			var self = this,
					tbody = $("<table></table>");
			self.getBodyTmp();
			tbody.addClass(self.classes);
			tbody.attr("id",self.id);
			tbody.append(self.bodyTmp);
			self.body.append(tbody);
		},
		//获取tfoot的内容
		getFooterTmp:function(){
			var self = this;
			self.footerTmp = self.target.find("tfoot").clone(); 
		},
		//创建tfoot
		creatFooter:function(){
			var self = this;
			var ftable = $("<table></table>");
			self.getFooterTmp();
			ftable.addClass(self.classes);
			ftable.append(self.footerTmp);
			self.footer.append(ftable);
		//	self.body.find("tfoot").hide();
		},
		getPaginationTmp:function(cb){
			var self = this;
			$.ajax({
				type:"get",
				url:self.config.pagination_url,
				success:function(data){
					self.paginationTmp = data;
					cb&&cb(data);
				}
			});
		},
		//根据模版设置分页栏
		creatPagination:function(){
			var self = this;
			self.getPaginationTmp(function(data){
				self.pagination.append(data);
		//		$("body").append(self.pagination);
			});
		},
		//根据id创建工具栏
		creatToolBar:function(){
			var self = this;
			var toolBarIdTmp = "";
			var toolBarId = self.config.toolBarId;
			if(toolBarId){
				toolBarIdTmp = $("#"+toolBarId).html();
				self.toolbar.html(toolBarIdTmp);
				$("body").append(self.toolbar);
			}
		},
		getParmByAttr:function(attr){
			return this.target.data(attr) || this.target.attr("data-"+attr);
		},
		//初始化，设定的参数
		initParm:function(){
			var self = this;
			self.config = {};
			self.config.height = self.getParmByAttr("height");
			self.pagination_url = self.getParmByAttr("pagination_url");
			self.pagination = self.getParmByAttr("pagination");
			self.toolBarId = self.getParmByAttr("toolBarId");
		},
		iniTable:function(){
			var self = this;
//			$("body").append(self.header);

			if(self.hasCaption){
				//创建caption
				self.creatCaption();
				self.tableWraper.append(self.caption);
			}
			
			if(self.hasThead){
				self.target.find("thead").css({
					"visibility":"hidden",
				});
				//创建header html
				self.creatHeader();
				self.container.append(self.header);
				self.header.find("thead").css({
					"visibility":"visible",
				});
			}
			
			if(self.hasTfoot){
				//将原table的tfoot设置隐藏
				self.target.find("tfoot").css({
					"visibility":"hidden",
				});
				//创建footer html，并设置可见
				self.creatFooter();
				self.footer.find("tfoot").css({
					"visibility":"visible"
				});
				self.container.append(self.footer);
			}
			
			
			
			//创建body html
			self.creatBody();
			self.container.append(self.body);
			self.body.find("tfoot").css({
				"visibility":"hidden",
			});
			
			//创建分页栏
			if(self.config.pagination_url){
				self.creatPagination();
				self.container.append(self.pagination);
			}
			//创建工具栏
			self.creatToolBar();
			self.tableWraper.append(self.toolbar);
			
			self.tableWraper.append(self.container);
			
			self.tableWraper.append("<div class='clearfix'></div>");
			//根据原table的id设置tableWraper的id
			self.tableWraperId = "tableWraper_"+self.id ;
			self.tableWraper.attr("id",self.tableWraperId);
			//在原table前插入 新html
			$(self.target).before(self.tableWraper);
			//移出原table
			$(self.target).remove();
			
			//表头，表尾的定时自适应
			self.timer = "";
			self.timer = setInterval(function(){
				self.initResize();
			},500);
			setTimeout(function(){
				clearInterval(self.timer);
			},10000);
		},
		initResize:function(){
			var self = this,
				id = self.tableWraperId,
				toolbar = self.toolbar,
				container = self.container,
				header = self.header,
				body = self.body,
				footer = self.footer,
				pagination = self.pagination;
			
			var tableJson = {};
			var basicTable = $("#"+self.id);
			//表格的宽度
			tableJson.width = basicTable.width()+2;
			//caption的高度
			tableJson.captionH = basicTable.find("caption").outerHeight();
			//thead的整体高度
			tableJson.theadH =  basicTable.find("thead").outerHeight();
			//tfoot的整体高度
			tableJson.tfootH =  basicTable.find("footer").outerHeight();
			body.css({"margin-top":tableJson.theadH});
			if(self.config.height){
				body.css({
					"height":self.config.height - tableJson.captionH - tableJson.theadH - 4
				})
			}
			self.initHeader();
			
			
			self.initFooter();
			
		},
		//header自适应
		initHeader:function(){
			var self = this,
					header = self.header
			var basicTable = $("#"+self.id);
			var tableJson = {};
			//表格的宽度
			tableJson.width = basicTable.width()+2;
			//caption的高度
			tableJson.captionH = basicTable.find("caption").outerHeight();
			//thead的整体高度
			tableJson.theadH =  basicTable.find("thead").outerHeight();
			//thead的行宽度和高度
			var theadArr = [];
			basicTable.find("thead tr").each(function(){
				var trJson = {};
				var thArr = [];
				$(this).children("th").each(function(){
					thArr.push($(this).outerWidth());
				});
				trJson.height = $(this).outerHeight();
				trJson.ths = thArr;
				theadArr.push(trJson);
			});
			
			header.css({"width":tableJson.width});
			basicTable.css({"margin-top":-tableJson.theadH - tableJson.captionH});
			var newHeadTable = header.find("table");
			newHeadTable.find("tr").each(function(n){
				var trData = theadArr[n];
				$(this).css({
					"height":trData.height
				})
				$(this).children("th").each(function(m){
					$(this).css({
						"width":trData.ths[m]
					})
				});
			});
		},
		//footer自适应
		initFooter:function(){
			var self = this,
					footer = self.footer;
			var basicTable = $("#"+self.id);
			var tableJson = {};
			//表格的宽度
			tableJson.width = basicTable.width()+2;
			tableJson.tfootH = basicTable.find("tfoot").outerHeight();
			//thead的行宽度和高度
			var theadArr = [];
			basicTable.find("tfoot tr").each(function(){
				var trJson = {};
				var thArr = [];
				$(this).children("td").each(function(){
					thArr.push($(this).outerWidth());
				});
				trJson.height = $(this).outerHeight();
				trJson.ths = thArr;
				theadArr.push(trJson);
			});
			
			footer.css({
				"width":tableJson.width
			});
			var newHeadTable = footer.find("table");
			newHeadTable.find("tr").each(function(n){
				var trData = theadArr[n];
				/*$(this).css({
					"height":trData.height
				})*/
				$(this).children("td").each(function(m){
					$(this).css({
						"width":trData.ths[m]
					})
				});
			});
		},
		winResize:function(){
			
		}
	}
	win.MaxTable = OTable;
	win.tableInit = function(collection){
		collection.each(function(){
			console.log($(this));
			new OTable($(this));
		});
	}
})(window,jQuery);